﻿using AntdUI;
using System.ComponentModel.Design;
using System.Drawing.Design;
using Button = AntdUI.Button;

namespace WenAntdUI.Controls;

/// <summary>
/// 搜索按钮
/// </summary>
[ToolboxItem(true)]
[DefaultEvent(nameof(QueryClick))]
public class InputQuery : IControl
{
    #region 私有属性
    private readonly Button refreshButton;
    private readonly Button queryButton;
    private readonly Input inputTextBox;
    #endregion

    public InputQuery()
    {
        refreshButton = new Button()
        {
            Dock = DockStyle.Left,
            Text = "刷新",
            AutoSizeMode = TAutoSize.Width,
            JoinRight = true,
            BorderWidth = 1,
            IconSvg = "ReloadOutlined"
        };
        queryButton = new Button()
        {
            Dock = DockStyle.Right,
            Text = "搜索",
            AutoSizeMode = TAutoSize.Width,
            JoinLeft = true,
            BorderWidth = 1,
            IconSvg = "SearchOutlined",
        };
        inputTextBox = new Input()
        {
            Dock = DockStyle.Fill,
            JoinLeft = true,
            JoinRight = true,
            BorderWidth = 1,
        };

        this.Controls.Add(inputTextBox);
        this.Controls.Add(refreshButton);
        this.Controls.Add(queryButton);

        inputTextBox.KeyDown += (s, e) => { if (e.KeyCode == Keys.Enter) OnQueryClick(inputTextBox.Text); };
        refreshButton.Click += (s, e) => OnQueryClick();
        queryButton.Click += (s, e) => OnQueryClick(inputTextBox.Text);

    }

    #region 共有属性
    /// <summary>
    /// 是否显示搜索按钮
    /// </summary>
    [Description("是否显示搜索按钮")]
    [Category("外观")]
    [DefaultValue(true)]
    public bool IsShowQueryButton { get => refreshButton.Visible; set => this.refreshButton.Visible = value; }
    /// <summary>
    /// 是否显示刷新按钮
    /// </summary>
    [Description("是否显示刷新按钮")]
    [Category("外观")]
    [DefaultValue(true)]
    public bool IsShowRefreshButton { get => queryButton.Visible; set => this.queryButton.Visible = value; }
    /// <summary>
    /// 搜索按钮点击事件 订阅
    /// </summary>
    public event EventHandler<string>? QueryClick;
    /// <summary>
    /// 按钮点击后事件 委托
    /// </summary>
    public Func<string, Task>? QueryChanged { get; set; }
    #endregion

    #region 重写属性
    [Description("文本")]
    [Category("外观")]
    [DefaultValue("")]
    [Editor(typeof(MultilineStringEditor), typeof(UITypeEditor))]
    public new string Text { get => inputTextBox.Text; set => inputTextBox.Text = value; }
    #endregion

    public virtual void OnQueryClick(string text = "")
    {
        QueryClick?.Invoke(this, text);
        QueryChanged?.Invoke(text);
    }
}
